package sbc.jms;


import javax.annotation.Resource;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.EJB;
import javax.ejb.MessageDriven;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;

import org.jboss.logging.Logger;

import sbc.ejb3.Platine;
import sbc.ejb3.Sensor;
import sbc.ejb3.dao.PlatineRemote;
import sbc.ejb3.dao.SensorRemote;

@MessageDriven(activationConfig =
{     
      @ActivationConfigProperty(propertyName="destinationType", propertyValue="javax.jms.Queue"),
      @ActivationConfigProperty(propertyName="destination", propertyValue="queue/bridgeQueue"),
      @ActivationConfigProperty(propertyName="DLQMaxResent", propertyValue="2")
})
public class BridgeListener implements MessageListener {

	static Logger log = Logger.getLogger(BridgeListener.class);
	
	@Resource(mappedName="TopicConnectionFactory")
	private TopicConnectionFactory topicConnectionFactory;
	
	@Resource(mappedName="topic/qsTopic")
	private Topic qsTopic;
	
    @EJB private PlatineRemote platineDAO;
    @EJB private SensorRemote sensorDAO;
	

	public BridgeListener(){
		log.debug("BridgeListener MDB constructed");
	}	

    public void onMessage(Message m) {
		log.info("message received");
	
		try {
			/*
			 * read message
			 */
			ObjectMessage message = (ObjectMessage)m;
			Object object = message.getObject();
			
			/*
			 * insert platine/sensor into databse
			 */
			Sensor sensor = null;
			if (object instanceof sbc.model.Platine) {
				sbc.model.Platine p = (sbc.model.Platine)object;
				log.info("received platine: " + p.getMyId());
				
				Platine platine = new Platine(p.getMyId());
				platineDAO.persist(platine);
			}
			else if (object instanceof sbc.model.Sensor) {
				sbc.model.Sensor s = (sbc.model.Sensor)object;
				log.info("received sensor: " + s.getMyId());
				
				sensor = new Sensor(s.getMyId());
				sensor = sensorDAO.merge(sensor);;
			}

			/*
			 * forward sensor to QS Topic
			 */
			if (sensor != null) {
				log.info("forwarding sensor to qsTopic");
				
				log.info("Creating connection");
				TopicConnection connection = topicConnectionFactory.createTopicConnection();
	
		        try {
					log.info("Creating session");
					TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
					
					log.info("Creating publisher");
					TopicPublisher sender = session.createPublisher(qsTopic);
	
					log.info("Creating message");					
					ObjectMessage topicObjectMessage = session.createObjectMessage(sensor);
		
					log.info("Sending message");
					sender.send(topicObjectMessage);
		        }
		        finally {
		        	connection.close();
			    }        
			}
		}
		catch(Exception e) {
	        log.error("error during message forwarding: " + e);
		}
    }
}

